package translinkApp.DialogBoxes.GoCard;

import translinkApp.Activities.R;
import translinkApp.views.dialog.LachDialog;
import translinkApp.views.dialog.LachDialogArguments;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class GoCardDetailsDialog extends LachDialog<GoCardDetailsDialogData> {

	private Context context;

	private EditText cardNumber;
	private EditText cardPassword;

	private Button saveButton;
	private Button cancelButton;

	private String getCardNumber() {
		return cardNumber.getText().toString();
	}

	private String getCardPassword() {
		return cardPassword.getText().toString();
	}

	public GoCardDetailsDialog(final Context context, LachDialogArguments args,
			GoCardDetailsDialogData saveDialogData) {

		super(context, R.layout.gocarddetailsdialog, args, saveDialogData);
		this.context = context;

		setTitle("Go-Card credentials                    ");

		cardNumber = (EditText) findViewById(R.id.gocardNumber);
		cardPassword = (EditText) findViewById(R.id.gocardPassword);

		saveButton = (Button) findViewById(R.id.saveJourneySave);
		cancelButton = (Button) findViewById(R.id.saveJourneyCancel);

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(((Activity) context).getBaseContext());

		String cardNumValue = prefs.getString("cardNum", null);
		String cardNumPass = prefs.getString("cardPass", null);
		if (cardNumValue != null) {
			cardNumber.setText(cardNumValue);
		}
		if (cardNumPass != null) {
			cardPassword.setHint("Password not changed");
		}

		cancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				cancel();
			}

		});

		saveButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Save()) {
					cancel();
				}
			}
		});

		if (saveDialogData != null) {
			loadDialogData(saveDialogData);
		}

	}

	public boolean Save() {

		if ((getCardNumber().length() == 0) || (getCardPassword().length() == 0)) {
			translinkApp.Utilities.Utils.showAlertDialog(context,
					"You must enter a card number and a password");
			return false;
		}

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(((Activity) context).getBaseContext());

		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("cardNum", getCardNumber());
		editor.putString("cardPass", getCardPassword());
		editor.commit();

		Toast.makeText(this.context, "Go-Card details saved.", Toast.LENGTH_SHORT).show();

		return true;
	}

	@Override
	public GoCardDetailsDialogData getDialogData() {
		return new GoCardDetailsDialogData(cardNumber.getText().toString(), cardPassword.getText()
				.toString());
	}

	@Override
	public void loadDialogData(GoCardDetailsDialogData data) {
		cardNumber.setText(data.getUserName());
		cardPassword.setText(data.getPassword());

	}
}